package com.njrsrc.android.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.app.JobDetailActivity;
import com.njrsrc.android.bean.JobInfo;
import com.njrsrc.common.utils.Helpers;

public class CompanyPositionListAdapter extends BaseAdapter {

	private List<JobInfo> list = new ArrayList<JobInfo>();
	private Context mContext;

	public CompanyPositionListAdapter(Context mContext) {
		this.mContext = mContext;
	}

	public void setData(ArrayList<JobInfo> data) {
		if (data != null) {
			for (JobInfo operateEntry : data) {
				list.add(operateEntry);
			}
		}
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		Holder holder = null;
		if (convertView == null) {
			holder = new Holder();
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.item_list_position, null);
			holder.companyTextView = (TextView) convertView.findViewById(R.id.item_job_company);
			holder.timeTextView = (TextView) convertView.findViewById(R.id.item_job_time);
			holder.addrTextView = (TextView) convertView.findViewById(R.id.item_job_addr);
			holder.titleTextView = (TextView) convertView.findViewById(R.id.item_job_title);
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.item_job_layout);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.companyTextView.setText(list.get(position).getCompany().getName());
		String time = Helpers.getStandardTimeStr(mContext, list.get(position).getModified_at());
		holder.timeTextView.setText(time);
		holder.addrTextView.setText(list.get(position).getArea());
		holder.titleTextView.setText(list.get(position).getTitle());
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 进入详情页面只需要传一个工作ID
				String job_id = list.get(position).getJob_id();
				Intent detailIntent = new Intent();
				detailIntent.setClass(mContext, JobDetailActivity.class);
				detailIntent.putExtra("job_id", job_id);
				mContext.startActivity(detailIntent);
			}
		};
		holder.layout.setOnClickListener(listener);
		return convertView;
	}

	class Holder {
		TextView companyTextView;
		TextView timeTextView;
		TextView addrTextView;
		/** 工作名称 */
		TextView titleTextView;
		RelativeLayout layout;
	}
}
